﻿using System;
using System.Web.Mail;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using MLY;
using MLY.Dal;
using MLY.Bll;
using System.IO;

namespace MLY.Reports.SCCoverReports
{
    public partial class OpenUserAlerts : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UserAlertsDataSource.SelectParameters.Add("UserId", SessParam.UserId.ToString());
                ddlOrgs_Load();
                ReloadOrgs(); 
                ReloadReport();
            }
        }

        protected void ReloadReport(object sender, EventArgs e)
        {
            ReloadReport();
        }

        protected void ReloadOrgs(object sender, EventArgs e)
        {   
            ReloadOrgs();
        }

        protected void DatesChanged(object sender, EventArgs e)
        {
            DatesPanel.Visible = (DatesLimitRBL.SelectedValue == "1");                
        }

        protected void alertRBLChange(object sender, EventArgs e)
        {
            UserAlertsDDL.Visible = (AlertIdRBL.SelectedValue == "1");
        }

        private void ReloadOrgs()
        {
            string OrgsRBL = OrganizationsRadioButtonList.SelectedValue;
            switch (OrgsRBL)
            { 
                case "0":
                    ddlContractors.Visible = false;
                    ddlCustomers.Visible = false;
                    break;
                case "1":
                    ddlContractors.Visible = true;
                    ddlCustomers.Visible = false;
                    break;
                case "2":
                    ddlContractors.Visible = false;
                    ddlCustomers.Visible = true;
                    break;
                default:
                    ddlContractors.Visible = false;
                    ddlCustomers.Visible = false;
                    break;
            }
        }

        private void ReloadReport()
        {
            string OrgId;
            string OrgRBLValue = OrganizationsRadioButtonList.SelectedValue;
            string Status = OpenClosedRDL.SelectedValue;
            string StartDate = "2000-01-01";
            string EndDate = "2030-01-01";
            string LimitDate = "0";
            bool StatusOpen = (Status == "1");
            
            string buttonText = StatusOpen ? "Close selected alerts" : "Delete selected alerts";
            CloseSelectedButton.Text = buttonText;
            CloseSelectedButton2.Text = buttonText;
            
            switch (OrgRBLValue)
            { 
                case "0":
                    OrgId = "0";
                    break;
                case "1":
                    OrgId = ddlContractors.SelectedValue;
                    break;
                case "2":
                    OrgId = ddlCustomers.SelectedValue;
                    break;
                default:
                    OrgId = "0";
                    break;
            }

            // Checking dates:
            if (DatesLimitRBL.SelectedValue == "1")
            {
                StartDate = MLY.Utils.SqlDate(DateTime.Parse(txtFromDate.Text.Trim()));
                EndDate = MLY.Utils.SqlDate(DateTime.Parse(txtToDate.Text.Trim()));
                LimitDate = DateTypeDDL.SelectedValue;
            }
            
            ReportDataSource.SelectParameters.Clear();
            ReportDataSource.SelectParameters.Add("UserId", SessParam.UserId.ToString());
            ReportDataSource.SelectParameters.Add("OrganizationId", OrgId);
            ReportDataSource.SelectParameters.Add("Status", Status);
            ReportDataSource.SelectParameters.Add("LimitDate", LimitDate);
            ReportDataSource.SelectParameters.Add("StartDate", StartDate);
            ReportDataSource.SelectParameters.Add("EndDate", EndDate);
            if (AlertIdRBL.SelectedValue == "1")
                ReportDataSource.SelectParameters.Add("AlertId", UserAlertsDDL.SelectedValue);

            ReportDataSource.DataBind();
            ReportGridview.DataBind();

            CloseSelectedButton.Visible = (ReportGridview.Rows.Count > 0);
            CloseSelectedButton2.Visible = (ReportGridview.Rows.Count > 0);
        }

        protected void CloseSelected(object sender, EventArgs e)
        {
            using (SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["MLYConnectionString"].ConnectionString))
            {
                SqlCommand cmd = new SqlCommand("SCCover_CloseRowMarking", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.Add("MarkingId", SqlDbType.Int);

                foreach (GridViewRow r in ReportGridview.Rows)
                {
                    try
                    {
                        if ((r.RowType == DataControlRowType.DataRow) &&
                            ((r.FindControl("CloseCheckBox") as CheckBox).Checked))
                        {
                            cmd.Parameters["MarkingId"].Value = (r.FindControl("MarkingIdHiddenField") as HiddenField).Value;
                            try
                            {
                                conn.Open();
                                cmd.ExecuteNonQuery();
                            }
                            catch { }
                            finally
                            {
                                conn.Close();
                            }
                        }
                    }
                    catch { }
                     
                }
            }
            ReloadReport();            
        }

        protected void ExportToExcel(object sender, EventArgs e)
        {
            MLY.Bll.ExcelExport.PrepareCsvFileFromGridview(ReportGridview, "OpenUserAlerts");
        }

        private void ddlOrgs_Load()
        {
            ddlContractors.AppendDataBoundItems = true;
            ddlCustomers.AppendDataBoundItems = true;
            
            ddlContractors.DataSource = MLY.Bll.clsOrganization.GetContractorsByClientId((int)Session["OrganizationId"]);
            ddlContractors.DataBind();
           
            ddlCustomers.DataSource = MLY.Bll.clsOrganization.GetClientsByContractorId((int)Session["OrganizationId"]);
            ddlCustomers.DataBind();

            if (ddlContractors.Items.Count > 0)
                OrganizationsRadioButtonList.Items.Add(new ListItem("Choose contractor", "1"));
            if (ddlCustomers.Items.Count > 0)
                OrganizationsRadioButtonList.Items.Add(new ListItem("Choose custromer", "2"));
        }
    }
}
